﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using filmquiz.Src.Domain;
using filmquiz.Src.DatabaseAccess;
using System.Collections.Generic;
using filmquiz.Src;

public partial class Stats : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["imageNumber"] != null)
        {
            Session["image"] = int.Parse(Request.Params["imageNumber"]);
        }

        notLoggedInLabel.Visible = false;
        notAnsweredLabel.Visible = false;
    }

    private void Page_PreRender(object sender, System.EventArgs e)
    {
        if (Session["loggedIn"] != null)
        {
            if ((Boolean)Session["LoggedIn"])
            {
                createQuizButtons();
                quizButtonWrapper.Visible = true;
                imageNo.Value = "ctl00_NavPlaceholder_butt" + (int)Session["image"];


                if ((int)Session["image"] != 0 && ((User)Session["user"]).getQuestions()[((int)Session["image"]) - 1])
                {
                    movieTitelLabel.Text = QuizHelper.getInstance().getAnswer((int)Session["image"]);

                    showPlayersThatAnswered();
                    showWrongAnswers();
                    showComments();

                    imageStatsDiv.Visible = true;
                    statsWrapper.Visible = true;
                    notAnsweredLabel.Visible = false;
                    generelStatsDiv.Visible = false;
                }
                else if ((int)Session["image"] != 0)
                {
                    notAnsweredLabel.Visible = true;
                    statsWrapper.Visible = false;
                }
                else if ((int)Session["image"] == 0)
                {
                    imageStatsDiv.Visible = false;
                    generelStatsDiv.Visible = true;
                    movieTitelLabel.Text = "";
                    statsWrapper.Visible = true;

                    showGenerelStats();
                    showNumberOfQuestionsAnswered();
                    showPlayersThatCompleted();
                }
            }
            else
            {
                statsWrapper.Visible = false;
                notLoggedInLabel.Visible = true;
                notAnsweredLabel.Visible = false;
                quizButtonWrapper.Visible = false;
            }
        }
        else
        {
            quizButtonWrapper.Visible = false;
            statsWrapper.Visible = false;
            notAnsweredLabel.Visible = false;
            notLoggedInLabel.Visible = true;
        }

        commentTextbox.Text = "";

    }

    /// <summary>
    /// Creates the number of buttons specified in Statics.cs
    /// </summary>
    private void createQuizButtons()
    {
        int numberOfQuestions = Statics.getInstance().getNumberOfQuestions();
        User user = (User)Session["user"];

        for (int i = 1; i <= numberOfQuestions; i++)
        {
            HyperLink hl = new HyperLink();
            hl.Text = i + "";
            hl.NavigateUrl = "Stats.aspx?imageNumber=" + i;
            hl.CssClass = "quizButton";
            hl.ID = "butt" + i;

            Label lbl = new Label();
            lbl.Text = "" + i;

            Image img = new Image();
            if (user.getQuestions()[i - 1])
            {
                img.ImageUrl = "Web_Resources/images/checkmark.png";
            }
            else
            {
                img.ImageUrl = "Web_Resources/images/questionsMark.png";
            }

            hl.Controls.Add(lbl);
            hl.Controls.Add(img);

            quizButtonsPlaceholder.Controls.Add(hl);
        }

        HyperLink hlg = new HyperLink();
        hlg.NavigateUrl = "Stats.aspx?imageNumber=0";
        hlg.CssClass = "generelStats";

        Label lblg = new Label();
        lblg.Text = "Rundens statistik";

        hlg.Controls.Add(lblg);

        quizButtonsPlaceholder.Controls.Add(hlg);
    }

    private void showPlayersThatAnswered()
    {
        List<String> usersWithCorrectAnswer = StatsHelper.getInstance().getPlayersThatAnsweredQuestion((int)Session["image"]);
        usersWithCorrectAnswer.Reverse();

        Label newLabel = new Label();
        newLabel.Text = "Brugere der har svaret rigtigt på dette billede:";
        newLabel.ID = "UsersHeaderLabel";
        newLabel.CssClass = "statsHeader";

        UsersPlaceHolder.Controls.Add(newLabel);
        UsersPlaceHolder.Controls.Add(new LiteralControl("<br />"));

        foreach (String name in usersWithCorrectAnswer)
        {
            newLabel = new Label();
            newLabel.Text = name;
            newLabel.ID = "Label" + name;

            UsersPlaceHolder.Controls.Add(newLabel);
            UsersPlaceHolder.Controls.Add(new LiteralControl("<br />"));
        }
    }

    private void showGenerelStats()
    {
        int correctAnswers = StatsHelper.getInstance().getNumberOfCorrectAnswers();
        int wrongAnswers = StatsHelper.getInstance().getNumberOfWrongAnswers();

        generelAnswersLabel.Text = "I denne runde har der i alt været " + (correctAnswers + wrongAnswers) + " svar. <br /> Ud af dem har " + correctAnswers + " været korrekte, og " + wrongAnswers + " været forkerte";
    }

    private void showNumberOfQuestionsAnswered()
    {
        List<int> numberOfQuestionsAnswered = StatsHelper.getInstance().getNumberOfPlayersThatAnsweredXQuestions();
        numberOfQuestionsAnswered.Reverse();

        Label newLabel = new Label();
        newLabel.Text = "Hvor mange brugere har svaret rigtigt på hvor mange spørgsmål:";
        newLabel.ID = "numberOfAnswersLabel";
        newLabel.CssClass = "statsHeader";

        numberOfQuestionsAnsweredPlaceholder.Controls.Add(newLabel);
        numberOfQuestionsAnsweredPlaceholder.Controls.Add(new LiteralControl("<br />"));

        for (int i = 0; i < Statics.getInstance().getNumberOfQuestions(); i++)
        {

            if (numberOfQuestionsAnswered[i] > 0)
            {
                newLabel = new Label();
                newLabel.Text = "Har svaret rigtigt på " + (Statics.getInstance().getNumberOfQuestions() - i) + " spørgsmål: " + numberOfQuestionsAnswered[i];
                newLabel.ID = "LabelNumberOfQuestionsAnswered" + i + numberOfQuestionsAnswered[i];

                numberOfQuestionsAnsweredPlaceholder.Controls.Add(newLabel);
                numberOfQuestionsAnsweredPlaceholder.Controls.Add(new LiteralControl("<br />"));
            }
        }
    }

    private void showPlayersThatCompleted()
    {
        List<String> playersThatCompleted = StatsHelper.getInstance().getPlayersThatCompleted();

        Label newLabel = new Label();
        newLabel.Text = "Brugere der har gættet alle billeder:";
        newLabel.ID = "CompletedHeaderLabel";
        newLabel.CssClass = "statsHeader";

        completedPlayersPlaceholder.Controls.Add(newLabel);
        completedPlayersPlaceholder.Controls.Add(new LiteralControl("<br />"));

        foreach (String name in playersThatCompleted)
        {
            newLabel = new Label();
            newLabel.Text = name;
            newLabel.ID = "Label" + name;

            completedPlayersPlaceholder.Controls.Add(newLabel);
            completedPlayersPlaceholder.Controls.Add(new LiteralControl("<br />"));
        }
    }

    private void showWrongAnswers()
    {
        List<WrongAnswer> wrongAnswers = StatsHelper.getInstance().getWrongAnswers((int)Session["image"]);

        Label newLabel = new Label();
        newLabel.Text = "10 mest hyppige forkerte svar til dette billede:";
        newLabel.ID = "LabelHeaderWrongAnswers";
        newLabel.CssClass = "statsHeader";

        WrongAnswersPlaceholder.Controls.Add(newLabel);
        WrongAnswersPlaceholder.Controls.Add(new LiteralControl("<br />"));

        int i = 0;
        foreach (WrongAnswer answer in wrongAnswers)
        {
            newLabel = new Label();
            newLabel.Text = answer.count + " - " + answer.answer;
            newLabel.ID = "Label" + answer.answer;

            WrongAnswersPlaceholder.Controls.Add(newLabel);
            WrongAnswersPlaceholder.Controls.Add(new LiteralControl("<br />"));
            i++;
            if (i == 10)
            {
                break;
            }
        }
    }

    private void showComments()
    {
        List<Comment> comments = StatsHelper.getInstance().getCommentsForQuestion((int)Session["image"]);

        Label newLabel = new Label();

        foreach (Comment comment in comments)
        {
            newLabel = new Label();
            newLabel.Text = comment.userName + " (" + comment.time + "): <br /> " + comment.comment;
            commentsPlaceholder.Controls.Add(new LiteralControl("<div class='comment'>"));
            commentsPlaceholder.Controls.Add(newLabel);
            commentsPlaceholder.Controls.Add(new LiteralControl("</div>"));
        }

    }

    protected void commentButton_Click(object sender, EventArgs e)
    {
        if (commentTextbox.Text.Length > 0)
        {
            StatsHelper.getInstance().insertComment((int)Session["image"], commentTextbox.Text, ((User)Session["user"]).getUserID());
        }
    }

}
